package com.hxd.otc.utils;


import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 作  者 :  卫广玉
 * 时  间 :  2018/6/12 15:18
 */
public class StringUtil {
    /**
     * @param number 需要转化的数字
     * @return 格式化小数位不进行四舍五入
     */
    public static String format2Number(double number) {
        return formatNumber(number, 2, false);
    }

    public static String format6Number(double number) {
        return formatNumber(number, 6, false);
    }

    public static String format4NumberWithoutZero(double number) {
        return formatNumber(number, 4, false);
    }

    /**
     * @param number 需要转化的数字
     * @return 格式化小数位不进行四舍五入
     */
    public static String format8Number(double number) {
        return formatNumber(number, 8, false);
    }

    /**
     * @param number 需要转化的数字
     * @return 格式化小数位不进行四舍五入
     */
    public static String format4Number(double number) {
        return formatNumber(number, 4, false);
    }

    public static String add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return formatNumberRetainZero(b1.add(b2).doubleValue(), 2, false);
    }

    public static String add(double v1, double v2,int accuracy ) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return formatNumberRetainZero(b1.add(b2).doubleValue(), accuracy, false);
    }

    public static long convertNumberToLong(double number) {
        return BigDecimal.valueOf(number).toBigInteger().longValue();
    }

    /**
     * @param number   需要转化的数字
     * @param scale 精度，表示需要保留的小数位
     * @param isHalfUp 是否四舍五入
     * @return 返回格式化小数位数的字符串，并去除小数点后的无效0
     */
    public static String formatNumber(double number, int scale, boolean isHalfUp) {
        return handleZero(formatNumberRetainZero(number, scale, isHalfUp));
    }

    /**
     * @param number   需要转化的数字
     * @param scale 精度，表示需要保留的小数位
     * @param isHalfUp 是否四舍五入
     * @return 返回格式化小数位数的字符串
     */
    public static String formatNumberRetainZero(double number, int scale, boolean isHalfUp) {
        return String.format("%." + scale + "f", BigDecimal.valueOf(number).setScale(scale, isHalfUp ? RoundingMode.HALF_UP : RoundingMode.DOWN));
    }

    public static String formatNumber(String numberStr, int newScale, RoundingMode roundingMode, boolean isStripTrailingZeros) {
        BigDecimal bigDecimal = new BigDecimal(numberStr).setScale(newScale, roundingMode);
        if (isStripTrailingZeros) {
            bigDecimal = bigDecimal.stripTrailingZeros();
        }
        return bigDecimal.toPlainString();
    }

    /**
     * @param numberStr 数字字符串
     * @return 返回去除小数点后的无效0的数字字符串
     */
    public static String handleZero(String numberStr) {
        if (TextUtils.isEmpty(numberStr)) return "0";
        try {
            return new BigDecimal(numberStr).stripTrailingZeros().toPlainString();
        } catch (Throwable e) {
            return "0";
        }
    }

    public static String nullIfEmpty(String str) {
        return TextUtils.isEmpty(str) ? null : str;
    }

    public static String emptyIfNull(String str) {
        return str == null ? "" : str;
    }

    public static SpannableString getSizeSpan(String content, int fontSize) {
        SpannableString sb = new SpannableString(content);
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(fontSize, true);//设置字体大小 true表示单位是sp
        sb.setSpan(ass, 0, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sb;
    }

    /**
     * 是否是一个数值
     */
    public static boolean isNumeric(String numericalStr) {
        return numericalStr.matches("-?[0-9]+.*[0-9]*");
    }

    /**
     * 是否是整数
     *
     * @param numericalStr
     * @return
     */
    public static boolean isInteger(String numericalStr) {
        return numericalStr.matches("-?[0-9]+");
    }

}
